#include "mysocket.h"
#include<QDataStream>
#include<QDebug>
#include"INCLUDE/user_nickName.h"
enum socketFlag{
    md5=1,
    versionFlag
};

Mysocket::Mysocket(qintptr p_sock,QObject *parent):QTcpSocket(parent)
{
    if(this->setSocketDescriptor(p_sock))
    {
        qDebug()<<"conn c succ!"<<endl;
    }
    else
    {
        qDebug()<<"conn c failed!"<<endl;
    }

    connect(this,SIGNAL(disconnected()),this,SLOT(onDiscon()));
    //this->forwordData();
    //当检测到有数据流入时，使用函数onRead()读取
    connect(this,SIGNAL(readyRead()),this,SLOT(onRead()));
    connect(this,SIGNAL(disconnected()),this,SLOT(onDiscon()));
}

Mysocket::~Mysocket()
{

}


void Mysocket::forwordData()
{
    //等待3秒，用以读取客户机发送的数据
    waitForReadyRead(3*1000);
    qDebug()<<"into forwordData"<<endl;

    int flag_c;
    char buf[1024]={0};
    read(buf,1024);
    if(strlen(buf)>0)
    {
        flag_c=this->write(buf,strlen(buf));//发送数据
        //flag_c==-1：socket传送数据成功
        qDebug()<<buf<<endl;
        qDebug()<<"write_flag"<<flag_c<<endl;
        if(flag_c==-1)
        {
            //将接收到的字符串和转发状态标志发送出去
            //emit dataReady(buf,flag_c);
            qDebug()<<"no data send!"<<endl;
        }
        else
        {
            qDebug()<<"write Msg:"<<buf<<endl;
        }
    }
}

void Mysocket::onDiscon()
{
    qDebug()<<"one client had disconnected!"<<endl;
}

void Mysocket::onRead()
{
    qDebug()<<" data is in!"<<endl;
    char buf[1024]={0};
    read(buf,1024);
    qDebug()<<buf<<endl;
    write(buf,strlen(buf));

    emit dataReady(buf);
}

void Mysocket::onReadUser()
{

}



